﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public abstract class EntityBase<T>
    {
        public abstract RESP_Info Add(T model);
        public abstract RESP_Info Update(T model);
        public abstract RESP_Info Delete(T id);

        public abstract T GetModel(string id);
        public abstract T GetModelBySQL(string sql);
        public abstract List<T> GetModels(string sql);

        public int GetCount(string sql)
        {
            int count = 0;
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        if (!reader.IsDBNull(0)) count = reader.GetInt32(0);
                    }
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("添加会员数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return count;
        }

        public RESP_Info ExecuteSQL(string sql)
        {
            RESP_Info info = new RESP_Info();
            
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    int i = cmd.ExecuteNonQuery();

                    cmd.Dispose();
                    if (i > 0)
                    {
                        info.Success = true;
                        info.Tip = "成功";
                        info.Title = "成功";
                    }
                    else
                    {
                        info.Success = false;
                        info.Tip = "失败";
                        info.Title = "失败";
                    }
                }
                catch (Exception ex)
                {
                    info.Success = false;
                    info.Tip = "失败";
                    info.Title = "失败";
                    Tom.Net.Log.LogManager.Error("执行数据库失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public void Binding(SqlDataReader reader, List<string> columns)
        { 
        
        }
    }
}
